package com.windward.vii.service.apns;

import com.notnoop.apns.APNS;
import com.notnoop.apns.ApnsService;
import com.notnoop.apns.EnhancedApnsNotification;
import com.windward.vii.dao.entity.AssetUserAccount;
import com.windward.vii.service.rongcloud.messages.BaseMessage;
import com.windward.vii.service.rongcloud.messages.extend.CallMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by tiansj on 2017/11/12.
 */
public class PushService {

    private static final Logger logger = LoggerFactory.getLogger(PushService.class);

    private static ApnsService apnsService;

    public static ApnsService getApnasService(boolean isProduction) {
        return APNS.newService()
                .withCert("/home/VOIP_Push_Certificates.p12", "111111")
                .withAppleDestination(isProduction)
                .build();
    }

    public static void sendPush(BaseMessage callMessage, String alertTitle, String deviceToken) throws Exception{
        sendPush(callMessage, alertTitle, deviceToken, true);
    }
    // 发送消息、消息结果、到达率
    public static void sendPush(BaseMessage callMessage, String alertTitle, String deviceToken, boolean isProduction) throws Exception{
        ApnsService  service = getApnasService(isProduction);
        try {
            String call = APNS.newPayload()
                    .badge(0)
                    .customField("msg_type", callMessage.getType())
                    .customField("msg_content", callMessage.toString())
                    .sound("ring.mp3")
                    .instantDeliveryOrSilentNotification()
                    .alertTitle(alertTitle)
                    .build();
            EnhancedApnsNotification notification = (EnhancedApnsNotification) service.push(deviceToken, call);
            logger.info("connect: PushPayload={}", call);
            logger.info("connect: PushResult={}", notification.toString());
            // String cancel = APNS.newPayload().customField("cmd","cancel").build();
            // service.push(deviceToken, cancel);
        } catch (Exception e) {
            logger.error("push throw Exception: " + e.getMessage());
            throw new Exception(e);
        } finally {
            if (service != null) {
                service.stop();
            }
        }
    }

    public static void cancelCallMessage(CallMessage callMessage, AssetUserAccount fromUser, String deviceToken)  throws Exception{
        cancelCallMessage(callMessage, fromUser, deviceToken, true);
    }
    public static void cancelCallMessage(CallMessage callMessage, AssetUserAccount fromUser, String deviceToken, boolean isProduction) throws Exception{
        ApnsService  service = getApnasService(isProduction);
        try {
            String call = APNS.newPayload()
                    .badge(3)
                    .customField("msg_type", CallMessage.TYPE)
                    .customField("msg_content", callMessage.toString())
                    .sound("ring.mp3")
                    .alertTitle(fromUser.getNickname() + " 邀请你进行视频通话")
                    .build();
            EnhancedApnsNotification notification = (EnhancedApnsNotification) service.push(deviceToken, call);
            logger.info("connect: PushPayload={}", call);
            logger.info("connect: PushResult={}", notification.toString());
            String cancel = APNS.newPayload().customField("cmd", "cancel").build();
            //  service.push(deviceToken, cancel);
        } catch (Exception e) {
            logger.error("push throw Exception: " + e.getMessage());
            throw new Exception(e);
        } finally {
            if (service != null) {
                service.stop();
            }
        }
    }
}
